//
//  LSLiveTopView.m
//  LookScore
//
//  Created by wr on 2018/3/26.
//  Copyright © 2018年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSLiveTopView.h"
#import "LSMarchDetailModel.h"
@implementation LSLiveTopView{
    
    UIImageView * _bgView;
    
    
    
    UIImageView * _leftImageView;
    UILabel * _hostLabel;
    UILabel * _hostRankLabel;
    
    
    UIImageView * _rightImageView;
    UILabel * _guestLabel;
    UILabel * _guestRankLabel;
    
    
    UILabel * _leagueNameLabel;
    
    UILabel * _scoreLabel;
    
    UILabel * _timeLabel;
    
    UILabel * _startLabel;
    
    UIImageView * _leftBgImageView;//主队图片背景框
    UIImageView * _rightBgImageView;//客队图片背景框
    
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self addContentView];
    }
    
    return self;
}


- (void) addContentView{
    _bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"liveBackGround"]];
    _bgView.backgroundColor = LSSubColor75b8fe;
    _bgView.userInteractionEnabled = YES;
    [self addSubview:_bgView];
    _bgView.sd_layout
    .leftEqualToView(self)
    .topEqualToView(self)
    .rightEqualToView(self)
    .bottomEqualToView(self);
    
    
    _leftBgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"homeBg"]];
    [_bgView addSubview:_leftBgImageView];
    _leftBgImageView.sd_layout
    .leftSpaceToView(_bgView, LSScareValue(20, LSScreenWidth))
    .centerYEqualToView(_bgView).offset(-10)
    .heightIs(LSScareValue(83, LSScreenWidth))
    .widthIs(LSScareValue(83, LSScreenWidth));
    
    _leftImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logoNull"]];
    [_leftBgImageView addSubview:_leftImageView];
    _leftImageView.sd_layout
    .centerXEqualToView(_leftBgImageView)
    .centerYEqualToView(_leftBgImageView)
    .heightIs(LSScareValue(70, LSScreenWidth))
    .widthIs(LSScareValue(70, LSScreenWidth));
    [CommonClass clipImageView:_leftImageView withCornerType:UIRectCornerAllCorners WithSize:CGSizeMake(35, 35)];
    
    _hostLabel = [[UILabel alloc] init];
    _hostLabel.textColor = LSTitleWhiteColor;
    _hostLabel.text = @"";
    _hostLabel.numberOfLines = 2;
    _hostLabel.font = LSFontSize12;
    _hostLabel.textAlignment = NSTextAlignmentCenter;
    [_bgView addSubview:_hostLabel];
    _hostLabel.sd_layout
    .centerXEqualToView(_leftBgImageView)
    .topSpaceToView(_leftBgImageView, 10)
    .autoHeightRatio(0)
    .widthIs(80);
//    .leftEqualToView(_leftImageView)
//    .rightEqualToView(_leftImageView);
//    [_hostLabel setSingleLineAutoResizeWithMaxWidth:180];
    
    _hostRankLabel = [[UILabel alloc] init];
    _hostRankLabel.textColor = LSTitleWhiteColor;
    _hostRankLabel.font = LSFontSize9;
    [_bgView addSubview:_hostRankLabel];
    
    _hostRankLabel.sd_layout
    .centerXEqualToView(_hostLabel)
    .topSpaceToView(_hostLabel, 5)
    .autoHeightRatio(0);
    [_hostRankLabel setSingleLineAutoResizeWithMaxWidth:180];
    
    
    
    
    _scoreLabel = [[UILabel alloc] init];
    _scoreLabel.font = LSFontBoldSize20;
    _scoreLabel.textColor= LSTitleWhiteColor;
    [_bgView addSubview:_scoreLabel];
    _scoreLabel.sd_layout
    .centerXEqualToView(_bgView)
    .centerYEqualToView(_bgView).offset(20)
    .autoHeightRatio(0);
    [_scoreLabel setSingleLineAutoResizeWithMaxWidth:180];
    
//    _timeLabel = [[UILabel alloc] init];
//    _timeLabel.font = LSFontSize12;
//    _timeLabel.textColor = LSTitleWhiteColor;
//    [_bgView addSubview:_timeLabel];
//    _timeLabel.sd_layout
//    .centerXEqualToView(_bgView)
//    .centerYEqualToView(_hostLabel)
//    .autoHeightRatio(0);
//    [_timeLabel setSingleLineAutoResizeWithMaxWidth:180];
    
    _startLabel = [[UILabel alloc] init];
    _startLabel.font = LSFontSize10;
    _startLabel.textColor = LSTitleWhiteColor;
    [_bgView addSubview:_startLabel];
    _startLabel.sd_layout
    .centerXEqualToView(_bgView)
    .centerYEqualToView(_hostLabel);
    [_startLabel setSingleLineAutoResizeWithMaxWidth:180];
    
    
    UIButton * btn = [[UIButton alloc] init];
    [btn setTitle:@"动画直播" forState:UIControlStateNormal];
    btn.titleLabel.font = LSFontSize12;
    [btn setTitleColor:LSTitleWhiteColor forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:btn];
    btn.sd_layout
    .centerXEqualToView(_bgView)
    .topSpaceToView(_startLabel, 5)
    .widthIs(60)
    .heightIs(25);
    
    _rightBgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guestBg"]];
    [_bgView addSubview:_rightBgImageView];
    _rightBgImageView.sd_layout
    .rightSpaceToView(_bgView, LSScareValue(20, LSScreenWidth))
    .centerYEqualToView(_bgView).offset(-10)
    .heightIs(LSScareValue(83, LSScreenWidth))
    .widthIs(LSScareValue(83, LSScreenWidth));
    
    _rightImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logoNull"]];
    [_rightBgImageView addSubview:_rightImageView];
    _rightImageView.sd_layout
    .centerXEqualToView(_rightBgImageView)
    .centerYEqualToView(_rightBgImageView)
    .heightIs(LSScareValue(70, LSScreenWidth))
    .widthIs(LSScareValue(70, LSScreenWidth));
    [CommonClass clipImageView:_rightImageView withCornerType:UIRectCornerAllCorners WithSize:CGSizeMake(35, 35)];
    
    
    _leagueNameLabel = [[UILabel alloc] init];
    _leagueNameLabel.textColor = LSTitleWhiteColor;
    _leagueNameLabel.font = LSFontSize14;
    _leagueNameLabel.textAlignment = NSTextAlignmentCenter;
    [_bgView addSubview:_leagueNameLabel];
    _leagueNameLabel.sd_layout
    .centerXEqualToView(_bgView)
    .centerYEqualToView(_bgView).offset(-45)
    .rightSpaceToView(_rightBgImageView, 0)
    .leftSpaceToView(_leftBgImageView, 0)
    .autoHeightRatio(0);
    
    _guestLabel = [[UILabel alloc] init];
    _guestLabel.font = LSFontSize12;
    _guestLabel.numberOfLines = 2;
    _guestLabel.text = @"  ";
    _guestLabel.textAlignment = NSTextAlignmentCenter;
    _guestLabel.textColor = LSTitleWhiteColor;
    [_bgView addSubview:_guestLabel];
    _guestLabel.sd_layout
    .centerXEqualToView(_rightBgImageView)
    .centerYEqualToView(_hostLabel)
    .autoHeightRatio(0)
    .leftEqualToView(_rightBgImageView)
    .rightEqualToView(_rightBgImageView);
//    [_guestLabel setSingleLineAutoResizeWithMaxWidth:180];
    
    _guestRankLabel = [[UILabel alloc] init];
    _guestRankLabel.font = LSFontSize9;
    _guestRankLabel.textColor = LSTitleWhiteColor;
    [_bgView addSubview:_guestRankLabel];
    
    _guestRankLabel.sd_layout
    .centerYEqualToView(_hostRankLabel)
    .centerXEqualToView(_guestLabel)
    .autoHeightRatio(0);
    [_guestRankLabel setSingleLineAutoResizeWithMaxWidth:180];
    
}
- (void)setDict:(NSDictionary *)dict{
    _dict = dict;
    
    
//    MatchState
    if (dict[@"MatchState"] == 0) {
        _scoreLabel.text = @"未开";
        
    }else{
       _scoreLabel.text = [NSString stringWithFormat:@"%@  :  %@",dict[@"HomeTeamScore"],dict[@"GuestTeamScore"]];
    }
    
    
}
- (void)setModel:(LSMarchDetailModel *)model{
    _model = model;
  
    [_leftImageView sd_setImageWithURL:[NSURL URLWithString:TeamImageHeaderUrl((long)model.HomeTeamId)] placeholderImage:[UIImage imageNamed:@"logoNull"]];
    [_rightImageView sd_setImageWithURL:[NSURL URLWithString:TeamImageHeaderUrl((long)model.GuestTeamId)] placeholderImage:[UIImage imageNamed:@"logoNull"]];
    
    MyLog(@"homeId --- %@",TeamImageHeaderUrl((long)model.HomeTeamId));
    MyLog(@"guestId --- %@",TeamImageHeaderUrl((long)model.GuestTeamId));

    _hostLabel.attributedText = [CommonClass htmlStrTraslateNormalStrWithHtml:model.HomeTeamName_CN Withfont:LSFontSize12 withColor:LSTitleWhiteColor];
    _hostRankLabel.text = model.HomeTeamOrder;
    _guestLabel.attributedText = [CommonClass htmlStrTraslateNormalStrWithHtml:model.GuestTeamName_CN Withfont:LSFontSize12 withColor:LSTitleWhiteColor];
    _guestRankLabel.text = model.GuestTeamOrder;
    _leagueNameLabel.text = [NSString stringWithFormat:@"%@%@",[self splitTheString:model.MatchDateTime],model.LeagueName_CN];

    if (model.MatchState == 0) {
       _scoreLabel.text = @"未开";
    }else{
        _scoreLabel.text = [NSString stringWithFormat:@"%@  :  %@",model.HomeTeamScore,model.GuestTeamScore];
    }
    
    if (model.MatchState == -1) {
        _startLabel.text = @"结束";
    }
}

- (void)btnClicked:(UIButton *)sender{
    
    if (self.block) {
        self.block(sender);
    }
}

- (NSString *)splitTheString:(NSString *)string{
    
    NSMutableArray * array = [string componentsSeparatedByString:@"T"].mutableCopy;
    
    NSMutableArray * leftArray = [array.firstObject componentsSeparatedByString:@"-"].mutableCopy;
    NSMutableArray * rightArray = [array.lastObject componentsSeparatedByString:@":"].mutableCopy;
    [leftArray removeObjectAtIndex:0];
    [rightArray removeLastObject];
    
    
    return [NSString stringWithFormat:@"%@ %@",[leftArray componentsJoinedByString:@"-"],[rightArray componentsJoinedByString:@":"]];
    
    
    
}
@end
